home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_glimpse.idb / usr / freeware / src / glimpse-3.0 / libtemplate / util / log.c.z / log.c
C/C++ Source or Header  |  1997-09-09  |  6KB  |  278 lines

  1. static char rcsid[] = "$Id: log.c,v 1.15 1995/02/04 01:37:54 hardy Exp $";
  2. /*
  3.  *  log.c - Logging facilities for Essence system.
  4.  *
  5.  *  Darren Hardy, hardy@cs.colorado.edu, February 1994
  6.  *
  7.  *  ----------------------------------------------------------------------
  8.  *  Copyright (c) 1994, 1995.  All rights reserved.
  9.  *  
  10.  *          Mic Bowman of Transarc Corporation.
  11.  *          Peter Danzig of the University of Southern California.
  12.  *          Darren R. Hardy of the University of Colorado at Boulder.
  13.  *          Udi Manber of the University of Arizona.
  14.  *          Michael F. Schwartz of the University of Colorado at Boulder. 
  15.  *  
  16.  *  This copyright notice applies to all code in Harvest other than
  17.  *  subsystems developed elsewhere, which contain other copyright notices
  18.  *  in their source text.
  19.  *  
  20.  *  The Harvest software was developed by the Internet Research Task
  21.  *  Force Research Group on Resource Discovery (IRTF-RD).  The Harvest
  22.  *  software may be used for academic, research, government, and internal
  23.  *  business purposes without charge.  If you wish to sell or distribute
  24.  *  the Harvest software to commercial clients or partners, you must
  25.  *  license the software.  See
  26.  *  http://harvest.cs.colorado.edu/harvest/copyright,licensing.html#licensing.
  27.  *  
  28.  *  The Harvest software is provided ``as is'', without express or
  29.  *  implied warranty, and with no support nor obligation to assist in its
  30.  *  use, correction, modification or enhancement.  We assume no liability
  31.  *  with respect to the infringement of copyrights, trade secrets, or any
  32.  *  patents, and are not responsible for consequential damages.  Proper
  33.  *  use of the Harvest software is entirely the responsibility of the user.
  34.  *  
  35.  *  For those who are using Harvest for non-commercial purposes, you may
  36.  *  make derivative works, subject to the following constraints:
  37.  *  
  38.  *  - You must include the above copyright notice and these accompanying 
  39.  *    paragraphs in all forms of derivative works, and any documentation 
  40.  *    and other materials related to such distribution and use acknowledge 
  41.  *    that the software was developed at the above institutions.
  42.  *  
  43.  *  - You must notify IRTF-RD regarding your distribution of the 
  44.  *    derivative work.
  45.  *  
  46.  *  - You must clearly notify users that your are distributing a modified 
  47.  *    version and not the original Harvest software.
  48.  *  
  49.  *  - Any derivative product is also subject to the restrictions of the 
  50.  *    copyright, including distribution and use limitations.
  51.  */
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include <unistd.h>
  55. #include <errno.h>
  56. #include <time.h>
  57. #include <sys/file.h>
  58. #if defined(__STRICT_ANSI__)
  59. #include <stdarg.h>
  60. #else
  61. #include <varargs.h>
  62. #endif
  63. #include "util.h"
  64.  
  65. /* Local functions */
  66. static void standard_msg();
  67. static void log_flush();
  68.  
  69. /* Local variables */
  70. static FILE *fp_log = NULL;
  71. static FILE *fp_errs = NULL;
  72. static int pid;
  73. static char *pname = NULL;
  74.  
  75. #if defined(USE_LOG_SYNC) && defined(HAVE_FLOCK)
  76. static void lock_file(fp)
  77. FILE *fp;
  78. {
  79.     if (flock(fileno(fp), LOCK_EX) < 0)
  80.         log_errno("lockf");
  81.     if (fseek(fp, 0, SEEK_END) < 0)
  82.         log_errno("fseek");
  83. }
  84.  
  85. static void unlock_file(fp)
  86. FILE *fp;
  87. {
  88.     if (flock(fileno(fp), LOCK_UN) < 0)
  89.         log_errno("lockf");
  90. }
  91. #else
  92. #define lock_file(fp)        /* nops */
  93. #define unlock_file(fp)
  94. #endif
  95.  
  96. /*
  97.  *  init_log() - Initializes the logging routines.  log() prints to 
  98.  *  FILE *a, and errorlog() prints to FILE *b;
  99.  */
  100. void init_log(a, b)
  101. FILE *a, *b;
  102. {
  103.     fp_log = a;
  104.     fp_errs = b;
  105.     pid = getpid();
  106.     pname = NULL;
  107. }
  108.  
  109. void init_log3(pn, a, b)
  110. char *pn;
  111. FILE *a, *b;
  112. {
  113.     fp_log = a;
  114.     fp_errs = b;
  115.     pid = getpid();
  116.     pname = strdup(pn);
  117. }
  118.  
  119. /*
  120.  *  log() - used like printf(3).  Prints message to stdout.
  121.  */
  122. #if defined(__STRICT_ANSI__)
  123. void log(char *fmt,...)
  124. {
  125.     va_list ap;
  126.  
  127.     if (fp_log == NULL)
  128.         return;
  129.  
  130.     va_start(ap, fmt);
  131. #else
  132. void log(va_alist)
  133. va_dcl
  134. {
  135.     va_list ap;
  136.     char *fmt;
  137.  
  138.     if (fp_log == NULL)
  139.         return;
  140.  
  141.     va_start(ap);
  142.     fmt = va_arg(ap, char *);
  143. #endif                /* __STRICT_ANSI__ */
  144.     if (fp_log == NULL)
  145.         return;
  146.  
  147.     lock_file(fp_log);
  148.     standard_msg(fp_log);
  149.     vfprintf(fp_log, fmt, ap);
  150.     va_end(ap);
  151.     log_flush(fp_log);
  152.     unlock_file(fp_log);
  153. }
  154.  
  155. /*
  156.  *  errorlog() - used like printf(3).  Prints error message to stderr.
  157.  */
  158. #if defined(__STRICT_ANSI__)
  159. void errorlog(char *fmt,...)
  160. {
  161.     va_list ap;
  162.  
  163.     if (fp_errs == NULL)
  164.         return;
  165.  
  166.     va_start(ap, fmt);
  167. #else
  168. void errorlog(va_alist)
  169. va_dcl
  170. {
  171.     va_list ap;
  172.     char *fmt;
  173.  
  174.     if (fp_errs == NULL)
  175.         return;
  176.  
  177.     va_start(ap);
  178.     fmt = va_arg(ap, char *);
  179. #endif                /* __STRICT_ANSI__ */
  180.  
  181.     if (fp_errs == NULL)
  182.         return;
  183.  
  184.     lock_file(fp_errs);
  185.     standard_msg(fp_errs);
  186.     fprintf(fp_errs, "ERROR: ");
  187.     vfprintf(fp_errs, fmt, ap);
  188.     va_end(ap);
  189.     log_flush(fp_errs);
  190.     unlock_file(fp_errs);
  191. }
  192.  
  193. /*
  194.  *  fatal() - used like printf(3).  Prints error message to stderr and exits
  195.  */
  196. #if defined(__STRICT_ANSI__)
  197. void fatal(char *fmt,...)
  198. {
  199.     va_list ap;
  200.  
  201.     if (fp_errs == NULL)
  202.         exit(1);
  203.  
  204.     va_start(ap, fmt);
  205. #else
  206. void fatal(va_alist)
  207. va_dcl
  208. {
  209.     va_list ap;
  210.     char *fmt;
  211.  
  212.     if (fp_errs == NULL)
  213.         exit(1);
  214.  
  215.     va_start(ap);
  216.     fmt = va_arg(ap, char *);
  217. #endif                /* __STRICT_ANSI__ */
  218.  
  219.     if (fp_errs == NULL)
  220.         exit(1);
  221.  
  222.     lock_file(fp_errs);
  223.     standard_msg(fp_errs);
  224.     fprintf(fp_errs, "FATAL: ");
  225.     vfprintf(fp_errs, fmt, ap);
  226.     va_end(ap);
  227.     log_flush(fp_errs);
  228.     unlock_file(fp_errs);
  229.     exit(1);
  230. }
  231.  
  232. /*
  233.  *  log_errno() - Same as perror(); doesn't print when errno == 0
  234.  */
  235. void log_errno(s)
  236. char *s;
  237. {
  238.     if (errno != 0)
  239.         errorlog("%s: %s\n", s, strerror(errno));
  240. }
  241.  
  242.  
  243. /*
  244.  *  fatal_errno() - Same as perror()
  245.  */
  246. void fatal_errno(s)
  247. char *s;
  248. {
  249.     fatal("%s: %s\n", s, strerror(errno));
  250. }
  251.  
  252. /*
  253.  *  standard_msg() - Prints the standard pid and timestamp
  254.  */
  255. static void standard_msg(fp)
  256. FILE *fp;
  257. {
  258.     if (pname != NULL)
  259.         fprintf(fp, "%7s: ", pname);
  260.     else
  261.         fprintf(fp, "%7d: ", pid);
  262. #ifdef LOG_TIMES
  263.     {
  264.         time_t t = time(NULL);
  265.         char buf[BUFSIZ];
  266.  
  267.         strftime(buf, BUFSIZ - 1, "%y%m%d %H:%M:%S:", localtime(&t));
  268.         fprintf(fp, "%s ", buf);
  269.     }
  270. #endif
  271. }
  272.  
  273. static void log_flush(fp)
  274. FILE *fp;
  275. {
  276.     fflush(fp);
  277. }
  278.